package org.example.network.low.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Objects;
import java.util.Scanner;

public class EchoClient {
    public static void main(String[] args) throws IOException {
        // 1. 创建Socket实例，连接到服务器
        Socket socket = new Socket("127.0.0.1", 8080);

        // 2. 获取输入输出流
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String msg = scanner.nextLine();
            // 3. 向服务器发送数据
            out.println(msg);

            // 4. 读取服务器响应的数据
            String line = in.readLine();
            System.out.println("服务器响应：" + line);

            if(Objects.equals(line, "bye bye")){
                break;
            }
        }
        // 5. 关闭资源
        socket.close();
    }
}
